package com.embhealth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.embhealth.dao.MemberDao;
import com.embhealth.dao.OrderDao;
import com.embhealth.service.ReportService;
import com.embhealth.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 统计运营数据服务
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    /**查询运营数据统计
     * 根据页面数据返回信息用map容器封装对象
     * @return map
     * @throws Exception
     */
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        //初始化容器
        Map<String,Object> result = new HashMap<>();
        //报表日期
        String data = DateUtils.parseDate2String(DateUtils.getToday());
        result.put("reportDate",data);
        //当日新增会员数
        Integer memberCountByDate = memberDao.findMemberCountByDate(data);
        //总会员数
        Integer memberTotal = memberDao.findMemberTotalCount();
        //本周新增会员数(从周一开始)
        String monday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        String sunday = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());
        Integer memberCountAfterMonday = memberDao.findMemberCountAfterDate(monday);
        //本月新增会员数
        String data4Month = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        String date4LastMonth = DateUtils.parseDate2String(DateUtils.getLastDay4ThisMonth());
        Integer memberCountAfterMonth = memberDao.findMemberCountAfterDate(data4Month);
        //今日预约数
        Integer orderCountByDate = orderDao.findOrderCountByDate(data);
        //本周预约数
        Integer orderCountAfterMonday = orderDao.findOrderCountAfterDate(monday,sunday);
        //本月预约数
        Integer orderCountAfterMonth = orderDao.findOrderCountAfterDate(data4Month,date4LastMonth);
        //今日到诊数
        Integer visitsCountByDate = orderDao.findVisitsCountByDate(data);
        //本周到诊数
        Integer visitsCountByMonday = orderDao.findVisitsCountByDate(monday);
        //本月到诊数
        Integer visitsCountAfterMonth = orderDao.findVisitsCountAfterDate(data4Month);
        //查询热门套餐
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        result.put("reportDate",data);
        result.put("todayNewMember",memberCountByDate);
        result.put("totalMember",memberTotal);
        result.put("thisWeekNewMember",memberCountAfterMonday);
        result.put("thisMonthNewMember",memberCountAfterMonth);
        result.put("todayOrderNumber",orderCountByDate);
        result.put("thisWeekOrderNumber",orderCountAfterMonday);
        result.put("thisMonthOrderNumber",orderCountAfterMonth);
        result.put("todayVisitsNumber",visitsCountByDate);
        result.put("thisWeekVisitsNumber",visitsCountByMonday);
        result.put("thisMonthVisitsNumber",visitsCountAfterMonth);
        result.put("hotSetmeal",hotSetmeal);

        return result;
    }

   //后台数据统计
    public Map<String, Object> getOrderVisitDate() {
        Map<String,Object> map =new HashMap<>();
        //查询指定日期
        List<String>date=orderDao.findDate();
        System.out.println(date);
        map.put("date",date);
        //根据指定日期查询每日预约数
        List<Integer> orderCount=orderDao.findOrderByDate();
        map.put("orderCount",orderCount);
        //根据指定日期查询每日到诊数
        List<String>visitCount=orderDao.findVisitByDate();
        map.put("visitCount",visitCount);
        return map;
    }
}
